package com.swak.frame.filter;

import static com.swak.frame.filter.FilterConstants.DEFAULT_QPS;
import static com.swak.frame.filter.FilterConstants.FILTER_RATE_LIMIT_ENABLED;
import static com.swak.frame.filter.FilterConstants.FILTER_RATE_LIMIT_QPS;

import java.lang.annotation.Annotation;

import org.apache.commons.lang3.ArrayUtils;

import com.swak.frame.command.annotation.SwakExecutionType;
import com.swak.frame.dto.base.SysRestCode;
import com.swak.frame.filter.annotation.SwakMethod;
import com.swak.frame.ratelimit.annotation.RateLimit;
import com.swak.frame.ratelimit.event.RateAlarmEvent;
import com.swak.frame.ratelimit.event.RateLimitAlarmEvent;
import com.swak.frame.system.SystemConfigurable;
import com.swak.frame.ump.UmpTag;

/**
 * 根据swakMethod 获取限流和ump等注解信息
 * 
 * @ClassName: SwakMethodUtils.java
 * @author: colley.ma
 * @date: 2022/01/24
 */
public class SwakMethodUtils {

    public final static UmpTag filerUmpTag = new UmpTag() {
        @Override
        public Class<? extends Annotation> annotationType() {
            return UmpTag.class;
        }

        @Override
        public String value() {
            return "rpc";
        }

        @Override
        public String tag() {
            return "rpc";
        }

        @Override
        public boolean enableTP() {
            return true;
        }

        @Override
        public boolean enableHeartbeat() {
            return false;
        }
    };

    public static RateLimit getSwakMethodRateLimit(SwakMethod swakMethod,
        SystemConfigurable systemConfig) {
        if (ArrayUtils.isNotEmpty(swakMethod.rateLimit())) {
            return swakMethod.rateLimit()[0];
        }
        // 如果全局开启限流
        if (systemConfig.getBoolean(FILTER_RATE_LIMIT_ENABLED, false)) {
            return new RateLimit() {
                @Override
                public Class<? extends Annotation> annotationType() {
                    return RateLimit.class;
                }

                @Override
                public int value() {
                    return qps();
                }

                @Override
                public String retCode() {
                    return SysRestCode.SYS_REQ_LIMIT.getRetCode();
                }

                @Override
                public int qps() {
                    return systemConfig.getInteger(FILTER_RATE_LIMIT_QPS, DEFAULT_QPS);
                }

                @Override
                public String key() {
                    return null;
                }

                @Override
                public Class<? extends RateAlarmEvent>[] event() {
                    @SuppressWarnings("unchecked")
                    Class<? extends RateAlarmEvent>[] event =
                        new Class[] {RateLimitAlarmEvent.class};
                    return event;
                }

                @Override
                public String desc() {
                    return swakMethod.desc();
                }

                @Override
                public String fallbackMethod() {
                    return "";
                }

                @Override
                public SwakExecutionType async() {
                    return SwakExecutionType.SYNCHRONOUS;
                }
            };
        }
        return null;
    }
}
